import { CoolController, BaseController, CoolCommException } from '@cool-midway/core';
import { Body, Inject, Post } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { DemandHallEntity } from '../../entity/hall';
import { DemandHallService } from '../../service/hall';
import {
    PublishDemandHallDTO,
    UpdateDemandHallDTO,
    UpdateDemandHallStatusDTO,
    DemandHallPageDTO,
} from '../../dto/hall';

@CoolController({
    api: ['page', 'info'],
    entity: DemandHallEntity,
    service: DemandHallService,
})
export class ProviderDemandHallController extends BaseController {
    @Inject()
    ctx: Context;

    @Inject()
    demandHallService: DemandHallService;

    /**
     * 发布
     */
    @Post('/publish', { summary: '发布需求或运力' })
    async publish(@Body() dto: PublishDemandHallDTO) {
        await this.demandHallService.publish(this.ctx.provider.id, dto);
        return this.ok();
    }

    /**
     * 公共大厅列表
     */
    @Post('/page', { summary: '需求大厅列表' })
    async page(@Body() body?: DemandHallPageDTO) {
        return this.ok(await this.demandHallService.page(body ?? {}, this.ctx.provider?.id));
    }

    /**
     * 我的发布
     */
    @Post('/myPage', { summary: '我的发布列表' })
    async myPage(@Body() body) {
        return this.ok(
            await this.demandHallService.myPage(this.ctx.provider.id, body ?? {})
        );
    }

    /**
     * 更新状态
     */
    @Post('/updateStatus', { summary: '更新发布状态' })
    async updateStatus(@Body() dto: UpdateDemandHallStatusDTO) {
        if (!this.ctx.provider?.id) {
            throw new CoolCommException('登录失效');
        }
        await this.demandHallService.updateStatus(this.ctx.provider.id, dto);
        return this.ok();
    }

    /**
     * 编辑需求/运力
     */
    @Post('/updatePublish', { summary: '编辑需求或运力' })
    async updatePublish(@Body() dto: UpdateDemandHallDTO) {

        return this.ok(await this.demandHallService.updatePublish(this.ctx.provider.id, dto));
    }

    /**
     * 详情
     */
    //   @Post('/detail', { summary: '需求发布详情' })
    //   async detail(@Body('id') id: number) {
    //     if (!id) {
    //       throw new CoolCommException('ID不能为空');
    //     }
    //     return this.ok(await this.demandHallService.detail(id));
    //   }
}


